<?php if(get_option('ini_post')){?><div class="related-post">
<?php global $post;
  $post_num = get_option('ini_postnum');
  $tmp_post = $post;
  $tags = ''; $i = 0;
  if ( get_the_tags( $post->ID ) ) {
  foreach ( get_the_tags( $post->ID ) as $tag ) $tags .= $tag->name . ',';
  $tags = strtr(rtrim($tags, ','), ' ', '-');
  $myposts = get_posts('numberposts='.$post_num.'&tag='.$tags.'&exclude='.$post->ID);
  foreach($myposts as $post) { setup_postdata($post); if ($post->post_excerpt):$content=get_the_excerpt(); elseif (get_post_meta($post->ID,'description_value',true)): $content=trim(get_post_meta($post->ID,'description_value',true)); else:$content=mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 180,"..."); endif; echo post_list($content); $i ++; }
  }
  if ( $i < $post_num ) {
  $post = $tmp_post; setup_postdata($post);
  $cats = ''; $post_num -= $i;
  foreach ( get_the_category( $post->ID ) as $cat ) $cats .= $cat->cat_ID . ',';
  $cats = strtr(rtrim($cats, ','), ' ', '-');
  $myposts = get_posts('numberposts='.$post_num.'&category='.$cats.'&exclude='.$post->ID);
  foreach($myposts as $post) { setup_postdata($post); if ($post->post_excerpt):$content=get_the_excerpt(); elseif (get_post_meta($post->ID,'description_value',true)): $content=trim(get_post_meta($post->ID,'description_value',true)); else:$content=mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 180,"..."); endif; echo post_list($content); $i ++; }
  }
  if ( $i < $post_num ) {
  $myposts = get_posts('numberposts='.$post_num.'&orderby=rand&exclude='.$post->ID);
  foreach($myposts as $post) { setup_postdata($post); if ($post->post_excerpt):$content=get_the_excerpt(); elseif (get_post_meta($post->ID,'description_value',true)): $content=trim(get_post_meta($post->ID,'description_value',true)); else:$content=mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 180,"..."); endif; echo post_list($content); }
  }
  $post = $tmp_post; setup_postdata($post);
?>
<div class="clear"></div>
</div>
<?php }

function post_list($content){?>
  <a class="bd" href="<?php the_permalink() ?>" title="<?php echo $content;?>">
    <?php $thumb_img = has_post_thumbnail() ? get_the_post_thumbnail( $post->ID, array(110,110), array('alt' => get_the_title()) ) : get_post_img( 110, 110, 2);echo $thumb_img;?>
    <h2><?php the_title();?></h2>
  </a>
<?php  } ?>